/**
 *author:yhb
 */
package com.towker.frame.controller.rbac;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.towker.frame.model.rbac.Role;
import com.towker.frame.model.rbac.RoleUser;
import com.towker.frame.model.sys.User;
import com.towker.frame.service.rbac.*;
import com.towker.frame.service.rbac.role.RoleService;
import com.towker.frame.service.sys.user.UserService;
import com.towker.web.MyRequest;


@Controller
@RequestMapping("/manage/rbac")
public class RoleUserController
{
	// 角色用户关系业务对象。
	private RoleUserService service;
	private UserService userService;
	private RoleService roleService;

	public void setRoleUserService(RoleUserService service)
	{
		this.service = service;
	}

	public void setUserService(UserService userService)
	{
		this.userService = userService;
	}
	public void setRoleService(RoleService roleService){this.roleService = roleService;}


	/**
	 * 给角色分配用户。
	 */
	@RequestMapping("/assignRoleUser{viewName}")
	public ModelAndView assignRoleUser(HttpServletRequest request, HttpServletResponse response, @PathVariable String viewName) throws IOException
	{
		ModelAndView result = null;
		if ("1".equals(viewName))
		{
			result = new ModelAndView();
			result.setViewName("/manage/rbac/assignRoleUser.jsp");
		}
		else if ("2".equals(viewName))
		{
			MyRequest req = new MyRequest(request);
			PrintWriter out = response.getWriter();
			try
			{
				RoleUser po = new RoleUser();
				po.setSystemid(req.getLong("sysid"));
				po.setRoleid(req.getLong("roleid"));
				po.setUserid(req.getLong("userid"));
				service.add(po);
				out.print(1);
			}
			catch (Exception e)
			{
				e.printStackTrace();
				out.print(0);
			}
		}
		return result;
	}

	/**
	 * 给用户分配角色
	 */
	@RequestMapping("/assignUserRole{viewName}")
	public ModelAndView assignUserRole(HttpServletRequest request, HttpServletResponse response, @PathVariable String viewName) throws IOException
	{
		ModelAndView result = null;
		if ("1".equals(viewName))
		{
			result = new ModelAndView();
			result.setViewName("/manage/rbac/assignUserRole.jsp");
		}
		else if ("2".equals(viewName))
		{
			MyRequest req = new MyRequest(request);
			PrintWriter out = response.getWriter();
			try
			{
				User user = userService.getById(req.getLong("userid"));
				Role role = null;
				String[] arr = req.getString("sysRoleIds").split(",");
				if(arr.length>1)
				{
					String[] ids = arr[0].toString().split(":");
					role = roleService.getById(Long.parseLong(ids[1]));
				}
				else
				{
					String[] rid = req.getString("sysRoleIds").split(":");
					role = roleService.getById(Long.parseLong(rid[1]));
				}
				service.assignUserRole(req.getLong("userid"), req.getString("sysRoleIds"));
//				if (user.getUsertype() == 1||user.getUsertype() ==6)
//				{
//					if (role.getRoletype()==1)
//					{
//						service.assignUserRole(req.getLong("userid"), req.getString("sysRoleIds"));
//					}
//					else
//					{
//						out.print(2);
//						return result;
//					}
//				}
//				else if (user.getUsertype() == 2)
//				{
//					if (role.getRoletype()==2)
//					{
//						service.assignUserRole(req.getLong("userid"), req.getString("sysRoleIds"));
//					}
//					else
//					{
//						out.print(2);
//						return result;
//					}
//				}
//				else if(user.getUsertype() == 3)
//				{
//					
////					if (role.getRoletype()==4)
////					{
////						Organizer organizer = organizerService.getById(user.getOrganid());
////						if(organizer.getState()==2)
////						{
////							organizerService.updState(organizer.getId(), 3);
////							service.assignUserRole(req.getLong("userid"), req.getString("sysRoleIds"));
////						}
////						else
////						{
////							service.assignUserRole(req.getLong("userid"), req.getString("sysRoleIds"));
////						}
////						
////					}
////					else if(role.getRoletype()==3)
////					{
////						Organizer organizer = organizerService.getById(user.getOrganid());
////						System.out.println(organizer.getState());
////						if(organizer.getState()==3)
////						{
////							organizerService.updState(organizer.getId(), 2);
////							service.assignUserRole(req.getLong("userid"), req.getString("sysRoleIds"));
////						}
////						else
////						{
////							service.assignUserRole(req.getLong("userid"), req.getString("sysRoleIds"));
////						}
////						
////					}
////					else
////					{
////						out.print(2);
////						return result;
////					}
//				}
//				else if(user.getUsertype() == 4)
//				{
//					if (role.getRoletype()==2)
//					{
//						service.assignUserRole(req.getLong("userid"), req.getString("sysRoleIds"));
//					}
//					else
//					{
//						out.print(2);
//						return result;
//					}
//				}
//				else if(user.getUsertype() == 5)
//				{
//					if (role.getRoletype()==2)
//					{
//						service.assignUserRole(req.getLong("userid"), req.getString("sysRoleIds"));
//					}
//					else
//					{
//						out.print(2);
//						return result;
//					}
//				}
//				else if(user.getUsertype() == 7)
//				{
//					if (role.getRoletype()==6)
//					{
//						service.assignUserRole(req.getLong("userid"), req.getString("sysRoleIds"));
//					}
//					else
//					{
//						out.print(2);
//						return result;
//					}
//				}
				out.print(1);
			}
			catch (Exception e)
			{
				e.printStackTrace();
				out.print(0);
			}
		}
		return result;
	}

	/**
	 *通过编号移除角色用户分配。
	 */
	@RequestMapping("/delRoleUserById")
	public void delRoleUserById(HttpServletResponse response, HttpServletRequest request) throws IOException, Exception
	{
		MyRequest req = new MyRequest(request);
		PrintWriter out = response.getWriter();
		try
		{
			service.delById(req.getLong("id"));
			out.print(1);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			out.print(0);
		}
	}
}
